package com.sicheng.lc.周赛.分类.模拟.矩阵行走;

import java.util.Arrays;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/7/7 16:07
 */
public class 模拟行走机器人II {
    //https://leetcode.cn/problems/walking-robot-simulation-ii/
    class Robot {
        int m, n;
        String[] d = {"East", "North", "West", "South"};
        int[][] direction = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int k;
        int i, j;

        public Robot(int width, int height) {
            m = height;
            n = width;
        }

        int[] res = {0, 0};

        public void step(int num) {
            if (num % (2 * (m + n - 2)) == 0) {
                if (check(i, j)) {
                    k = (k + 3) % 4;
                }
                return;
            }
            num = num % (2 * (m + n - 2));
            while (num > 0) {
                int x, y;
                while (num > 0) {
                    x = i + direction[k][0];
                    y = j + direction[k][1];
                    if (x < 0 || x >= m || y < 0 || y >= n)
                        break;
                    num--;
                    i = x;
                    j = y;
                }
                if (num == 0)
                    break;
                k = (k + 1) % 4;
            }
            res[0] = j;
            res[1] = i;
        }

        private boolean check(int i, int j) {
            return
                    (i == 0 && j == 0 && k == 0) ||
                            (i == 0 && j == n - 1 && k == 1) ||
                            (i == m - 1 && j == n - 1 && k == 2) ||
                            (j == 0 && i == m - 1 && k == 3);

        }

        public int[] getPos() {
            return res;
        }

        public String getDir() {
            return d[k];
        }
    }

    public static void main(String[] args) {
        Robot robot = new 模拟行走机器人II().new Robot(6, 3);
        robot.step(2);
        robot.step(2);
        System.out.println(Arrays.toString(robot.getPos()));
        System.out.println(robot.getDir());
        robot.step(2);
        robot.step(1);
        robot.step(4);
        System.out.println(Arrays.toString(robot.getPos()));
        System.out.println(robot.getDir());


    }
}
